package 算法.力扣;

import java.util.Arrays;

/**
 * @author: 谢君臣
 * @create: 2020-09-26 14:18
 * @founction: XXXX
 */
public class SortList {
    public static void main(String[] args) {
        int[] A = {-4};
        int[] ints = sortedSquares(A);
        System.out.println(Arrays.toString(ints));
    }

    public static int[] sortedSquares(int[] A) {
        int i = 0, j = 0;
        int[] b = new int[A.length];
        if (A.length<2){
            A[0] = (int) Math.pow(A[0],2);
            return A;
        }
        while (A[i] < 0 && i < A.length) {
            j = i;
            i++;
        }
        // 归并排序
        int m = j, n = j + 1, t = 0;
        while (m >= 0 && n < A.length) {
            if (Math.pow(A[m],2) >= Math.pow(A[n],2))
                b[t++] = (int) Math.pow(A[n++],2);
            else
                b[t++] = (int) Math.pow(A[m--],2);
        }
        if (m < 0) {
            while (n < A.length)
                b[t++] = (int) Math.pow(A[n++],2);
        } else {
            while (m >= 0)
                b[t++] =(int) Math.pow(A[m--],2);
        }
        return b;
    }
}

